import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class ToolPage extends StatelessWidget {
  final List<ToolboxItem> items = [
    ToolboxItem(
      name: 'processOn绘图',
      logo: 'images/processOn.png',
      url: 'https://www.processon.com/',
    ),
    ToolboxItem(
      name: '图像处理网站',
      logo: 'images/图像处理.png',
      url: 'https://www.img2go.com/zh',
    ),
    ToolboxItem(
      name: '代码高亮工具',
      logo: 'images/代码高亮.png',
      url: 'https://tool.oschina.net/highlight',
    ),
    ToolboxItem(
      name: 'Linux命令手册',
      logo: 'images/Linux命令手册.png',
      url: 'https://www.linuxcool.com/',
    ),
    ToolboxItem(
      name: '电子书网站',
      logo: 'images/书籍.png',
      url: 'https://www.jiumodiary.com/',
    ),
    ToolboxItem(
      name: '数据集',
      logo: 'images/数据集.png',
      url: 'https://www.kaggle.com/',
    ),
    ToolboxItem(
      name: 'GPT',
      logo: 'images/GPT.png',
      url: 'https://chatgpt.com/',
    ),
    ToolboxItem(
      name: 'Gamma',
      logo: 'images/PPT.png',
      url: 'https://gamma.app/signin?redirect_to=https%3A%2F%2Fgamma.app%2Fgenerate',
    ),
    ToolboxItem(
      name: 'PDF工具',
      logo: 'images/PDF.png',
      url: 'https://www.ilovepdf.com/zh-cn',
    ),
    ToolboxItem(
      name: 'Latex公式编辑器',
      logo: 'images/latex.png',
      url: 'https://www.latexlive.com/##',
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('工具箱'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, // 每行两个工具箱
            childAspectRatio: 1, // 子项宽高比
            crossAxisSpacing: 8.0, // 子项水平间距
            mainAxisSpacing: 8.0, // 子项垂直间距
          ),
          itemCount: items.length,
          itemBuilder: (context, index) {
            final item = items[index];
            return Card(
              elevation: 4, // Adds a shadow
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0), // Rounded corners
              ),
              child: InkWell(
                onTap: () => _launchURL(item.url),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.circular(100.0), // Rounded corners for image
                      child: Image.asset(
                        item.logo,
                        width: 130, // Larger width
                        height: 130, // Larger height
                        fit: BoxFit.cover,
                      ),
                    ),
                    SizedBox(height: 10), // Space between image and text
                    Text(
                      item.name,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 24, // Larger font size
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  void _launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
}

class ToolboxItem {
  final String name;
  final String logo;
  final String url;

  ToolboxItem({
    required this.name,
    required this.logo,
    required this.url,
  });
}
